package fm.demo.smart.collection.speaker;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Data
public class TTSEntity {

    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String orgNum;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private List<Device> deviceList;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String msgId;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String bcType;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String voiceData;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String voiceType;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String volumn;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String transNo;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String timestamp;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
    private String sign;

    public String toSignTree(ObjectMapper objectMapper) throws JsonProcessingException {
        TreeMap<String, String> body = new TreeMap<>();
        List<TreeMap<String, String>> devices = new ArrayList<>();
        StringBuilder sb = new StringBuilder("[");
        for (Device device : deviceList) {
            sb.append("{")
                    .append("deviceIndex").append("=").append(device.getDeviceIndex())
                    .append(",")
                    .append("deviceId").append("=").append(device.getDeviceId())
                    .append("}").append(",");
        }
        String d = sb.substring(0, sb.length() - 1) + "]";
        body.put("orgNum", orgNum);
        body.put("deviceList", d);
        body.put("msgId", msgId);
        body.put("bcType", bcType);
        body.put("voiceData", voiceData);
        body.put("voiceType", voiceType);
        body.put("volumn", volumn);
        body.put("transNo", transNo);
        body.put("timestamp", timestamp);
        StringBuilder stringBuilder = new StringBuilder();
        body.forEach((k, v) -> {
            if (v != null && !"".equals(v)) {
                stringBuilder.append(k).append("=").append(v).append("&");
            }
        });
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }
}
